package org.listbeast.core.dataobjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * class that contains all artists + songs and provides convenient
 * methods for retrieving these
 * @param <E>
 * 
 */
public final class SongCollectionImpl<E extends Song> implements SongCollection<E> {

	private final HashMap<String, HashSet<E>> library = new HashMap<String, HashSet<E>>();

	public Set<String> getArtistNames() {
		return library.keySet();
	}

	public HashSet<E> getArtistSongs(String artistName) {
		return library.get(artistName);
	}

	public void addSong(E song) {
		HashSet<E> artistSongs = getArtistSongs(song.getArtistName());

		if (artistSongs == null) {
			// this is first song of artist. Need to create set.
			artistSongs = new HashSet<E>();
			artistSongs.add(song);
			library.put(song.getArtistName(), artistSongs);
		} else {
			artistSongs.add(song);
			library.put(song.getArtistName(), artistSongs);
		}
	}

	public List<E> getAllSongs() {
		List<E> allSongs = new ArrayList<E>();
		for (String artist : getArtistNames()) {
			allSongs.addAll(getArtistSongs(artist));
		}
		return allSongs;
	}

	public boolean contains(E song) {
		return getAllSongs().contains(song);
	}

}
